package cn.car91.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.PopupWindow;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.service.UpdateService;
import cn.car91.view.HerilyAlertDialog;

import com.kyleduo.switchbutton.SwitchButton;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class SetActivity extends BaseActivity {

    @ViewInject(R.id.set_navisound)
    SwitchButton naviSoundSwitchButton;
    @ViewInject(R.id.set_loadimg)
    SwitchButton loadImgSwitchButton;
    @ViewInject(R.id.set_logs)
    SwitchButton logsSwitchButton;

    SharedPreferences setPreference;
    Editor setEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_set);
        super.onCreate(savedInstanceState);

        setPreference = super.getCar91Application().getSetPreferences();
        setEditor = setPreference.edit();

        initView();

        ((TextView) findViewById(R.id.header_text)).setText(getResources().getString(R.string.menu_set));
    }

    private void initView() {
        boolean navi_sound = setPreference.getBoolean("navisound", false);
        boolean load_img = setPreference.getBoolean("loadimg", true);
        boolean push_logs = setPreference.getBoolean("pushlog", true);

        naviSoundSwitchButton.setChecked(navi_sound);
        loadImgSwitchButton.setChecked(load_img);
        logsSwitchButton.setChecked(push_logs);

        naviSoundSwitchButton.setOnCheckedChangeListener(onNaviSoundCheckedChangeListener);
        loadImgSwitchButton.setOnCheckedChangeListener(onLoadImgCheckedChangeListener);
        logsSwitchButton.setOnCheckedChangeListener(onLogsCheckedChangeListener);
    }

    private OnCheckedChangeListener onNaviSoundCheckedChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            setEditor.putBoolean("navisound", arg1);
            setEditor.commit();
        }
    };

    private OnCheckedChangeListener onLoadImgCheckedChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            setEditor.putBoolean("loadimg", arg1);
            setEditor.commit();
        }
    };

    private OnCheckedChangeListener onLogsCheckedChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            setEditor.putBoolean("pushlog", arg1);
            setEditor.commit();
        }
    };

    @OnClick(R.id.set_clearcache)
    public void onClearCacheClick(View view) {
        try {
            BitmapGlobalConfig.getInstance(this, "sdcard/car91/temp/article").clearCache();
        } catch (Exception e) {
            LogUtils.e("clear cache error");
        }
        try {
            BitmapGlobalConfig.getInstance(this, "sdcard/car91/temp/repairshop").clearCache();
        } catch (Exception e) {
            LogUtils.e("clear cache error");
        }
        try {
            BitmapGlobalConfig.getInstance(this, "sdcard/car91/temp/exposure").clearCache();
        } catch (Exception e) {
            LogUtils.e("clear cache error");
        }
        new HerilyAlertDialog.Builder(this).setTitle("提示").setMessage("清理完毕").create().show();
    }

    @OnClick(R.id.set_offlinemap)
    public void onOfflineMapClick(View view) {
        Intent intent = new Intent(this, OfflineMapActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.set_update)
    public void onUpdateClick(View view) {
        UpdateService.isFirst = false;
        startService(new Intent(this, UpdateService.class));
    }

    @OnClick(R.id.set_about)
    public void onAboutClick(View view) {
        Intent intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.set_recommend)
    public void onRecommendClick(View view) {
        //imageView.setLayoutParams(new LayoutParams(320,320));
        View view2 = getLayoutInflater().inflate(R.layout.activity_recommend, null);
        PopupWindow popupWindow = new PopupWindow(view2, LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, true);
        //popupWindow.setWindowLayoutMode(view.getRootView().getMeasuredWidth(), view.getRootView().getMeasuredHeight());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(view.getRootView(), Gravity.CENTER, 0, 0);
    }
}
